﻿using DG.Tweening;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class HpBar : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	} 

	List<GameObject> slots;

	public void Init() {

		int hpCount = SenceProp.PlayerHP;

		slots = new List<GameObject> (hpCount);

		GameObject parent = this.gameObject;
 
		GameObject slot;

		float step = 14;

		for (int i = 0; i < hpCount; i++) {

			slot = GameObject.Instantiate( Resources.Load<GameObject> ("Prefabs/HpSlot") );

			UIUtility.Alpha (slot, 0);

			UIUtility.SetAnchoredPosition (slot, 0 + step * i , 0);

			slot.transform.SetParent (parent.transform, false);

			slots.Add( slot );

		}

	}

	public void InitAnimation( TweenCallback callback ) {

		Sequence seq = DOTween.Sequence ();

		seq.OnComplete (callback);

		float count = slots.Count;

		float _d = 0.1f;
			
		for (int i = 0; i < count; i++) {

			seq.AppendInterval( _d );

			seq.Append ( slots[i].GetComponent<CanvasGroup>().DOFade (1, 0.01f) );

		}

	}


	public void UpdateHP() {

		for (int i = 0; i < SenceProp.PlayerHP; i++) {

			if (i < SenceProp.CurrentPlayerHP) {
				UIUtility.Alpha ( UIUtility.FindChild( slots [i] , "full" ) , 1);
			} else {
				UIUtility.Alpha ( UIUtility.FindChild( slots [i] , "full" ) , 0);
			}

		}

	}



}
